package com.aiyige.page.my.download;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.page.my.download.adapter.MyDownloadPagerAdapter;
import com.aiyige.page.my.download.event.EventAddLocalVideo;
import com.aiyige.page.my.download.event.EventRefreshDownloadProgress;
import com.aiyige.page.my.download.event.EventToggleEdit;
import com.aiyige.page.my.download.util.DownloadUtil;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SlidingTabLayout;
import com.luck.picture.lib.PictureSelector;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.MyDownloadPage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class MyDownloadPage extends AppCompatActivity {
    public static final int REQUEST_CODE_ADD_LOCAL_VIDEO = 1;
    @BindView(R.id.backBtn)
    ImageView backBtn;
    @BindView(R.id.editBtn)
    TextView editBtn;
    @BindView(R.id.cancelEditBtn)
    TextView cancelEditBtn;
    @BindView(R.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.mainVp)
    ViewPager mainVp;
    MyDownloadPagerAdapter pagerAdapter;
    List<DownloadDataPage> downloadDataPageList;
    DownloadDataPage learnVideoPage;
    DownloadDataPage normalVideoPage;
    DownloadDataPage localVideoPage;
    BroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_my_download);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        learnVideoPage = DownloadDataPage.newInstance(DownloadDataPage.FILTER_TYPE_LEARN_VIDEO);
        normalVideoPage = DownloadDataPage.newInstance(DownloadDataPage.FILTER_TYPE_NORMAL_VIDEO);
        localVideoPage = DownloadDataPage.newInstance(DownloadDataPage.FILTER_TYPE_LOCAL_VIDEO);
        downloadDataPageList = new LinkedList<>();
        downloadDataPageList.add(learnVideoPage);
        downloadDataPageList.add(normalVideoPage);
        downloadDataPageList.add(localVideoPage);
        pagerAdapter = new MyDownloadPagerAdapter(getSupportFragmentManager());
        pagerAdapter.setDownloadDataPageList(downloadDataPageList);
        mainVp.setAdapter(pagerAdapter);
        mainVp.setOffscreenPageLimit(downloadDataPageList.size());
        mainVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                showEditBtn();
                for (DownloadDataPage downloadDataPage : downloadDataPageList) {
                    downloadDataPage.getEventBus().postSticky(EventToggleEdit.newBuilder().enableEdit(false).build());
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                try {
                    String action = intent.getAction();
                    switch (action) {
                        case DownloadUtil.ACTION_DOWNLOAD_PROGRESS_STATUS_UPDATE:
                            normalVideoPage.getEventBus().post(new EventRefreshDownloadProgress());
                            learnVideoPage.getEventBus().post(new EventRefreshDownloadProgress());
                            break;
                        case DownloadUtil.ACTION_DOWNLOAD_PLAY_HISTORY_UPDATE:
                            normalVideoPage.getEventBus().post(new EventRefreshDownloadProgress());
                            learnVideoPage.getEventBus().post(new EventRefreshDownloadProgress());
                            localVideoPage.getEventBus().post(new EventRefreshDownloadProgress());
                            break;
                    }
                } catch (Exception e) {

                }
            }
        };
        tabLayout.setViewPager(mainVp);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(DownloadUtil.ACTION_DOWNLOAD_PROGRESS_STATUS_UPDATE);
        intentFilter.addAction(DownloadUtil.ACTION_DOWNLOAD_PLAY_HISTORY_UPDATE);
        registerReceiver(broadcastReceiver, intentFilter);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_ADD_LOCAL_VIDEO:
                if (resultCode == RESULT_OK) {
                    localVideoPage.getEventBus().postSticky(EventAddLocalVideo.newBuilder().localMediaList(PictureSelector.obtainMultipleResult(data)).build());
                }
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;

        }
    }

    public void showCancelEditBtn() {
        cancelEditBtn.setVisibility(View.VISIBLE);
        editBtn.setVisibility(View.INVISIBLE);
    }

    public void showEditBtn() {
        editBtn.setVisibility(View.VISIBLE);
        cancelEditBtn.setVisibility(View.INVISIBLE);
    }

    @OnClick({R.id.backBtn, R.id.editBtn, R.id.cancelEditBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backBtn:
                finish();
                break;
            case R.id.editBtn:
                showCancelEditBtn();
                downloadDataPageList.get(mainVp.getCurrentItem())
                        .getEventBus()
                        .postSticky(EventToggleEdit.newBuilder()
                                .enableEdit(true)
                                .build());
                break;
            case R.id.cancelEditBtn:
                showEditBtn();
                downloadDataPageList.get(mainVp.getCurrentItem())
                        .getEventBus()
                        .postSticky(EventToggleEdit.newBuilder()
                                .enableEdit(false)
                                .build());
                break;
        }
    }

    @Override
    protected void onDestroy() {
        try {
            unregisterReceiver(broadcastReceiver);
        } catch (Exception e) {

        }
        super.onDestroy();
    }
}
